/**

  Function definitions for the cputhrottle program

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "funcs.h"
#include "specs.h"

/**
  Opens a CPU frequency file and returns its contents as a long int.

  Returns -1 on error.
*/
long 
query_file(char* filename) 
{
    FILE* fp = fopen(filename, "r");

    if (!fp) {
        return -1;
    }

    long cpufreq;
    fscanf(fp, "%ld", &cpufreq);
    fclose(fp);
    return cpufreq;
}

/**
  Sets a (frequency) value into the CPU frequency file.
  Care must be taken to make sure the frequency value is within the
  acceptable limits of your machine. Check the limit files specified in
  `specs.h` for more information.

  Parameters:
   1. filename: a cpufreq file to which write the new freq value.
   2. value: a long int value in kHz of the freq to be set.

  Returns 0 on success, -1 on error (most likely permission).
*/
int
set_value(char* filename, long value)
{
    FILE* fp = fopen(filename, "w");

    if (!fp)
        return -1;

    fprintf(fp, "%ld\n", value);
    fclose(fp);
    return 0;
}
